home *** CD-ROM | disk | FTP | other *** search
- #include "packets.h"
- #include "atalk.h"
-
- #define ATP_TIMEOUT 2
- #define ATP_RETRY 1
- #define MAX_RESP_SIZE 512
-
- #define NBP_BUFFER_SIZE 4096
-
- #define NBP_SERVER_TYPE "Packet Monster"
-
- static nbpParam nbp_iopb;
- static namesTableEntry *nameEntry;
- static byte *lookupBuffer = (byte *)0;
-
- static word atpSocket;
- static at_BDSType bds[8];
-
- static byte requestBuff[MAX_RESP_SIZE];
- static byte responseBuff[MAX_RESP_SIZE];
-
- extern word theSize, theDelay;
- extern char theName[];
- extern char theZone[];
- extern uword theTestSize;
- extern word theBDSCount;
-
- InitThem()
- {
- /* open appletalk drivers */
- OpenAppleTalk();
- }
-
- SendThem()
- {
- NetAddress addr;
- word respSize, i, result;
- ulong requests = 0,
- packets = 0,
- errors = 0;
- ulong bytes = 0;
- ulong time, time2, time3;
- double seconds;
- ulong maxbytes = theTestSize * 1024L;
-
- if (FindServer(&addr))
- {
- for (i = 0; i < theSize; i++)
- requestBuff[i] = i;
-
- printf("server at net %d, node %d, socket %d\n",
- addr.na_network, addr.na_node, addr.na_socket);
-
- printf("test size %ld bytes\n", maxbytes);
-
- GetDateTime(&time2);
- time = Ticks;
- while (!Button())
- {
- result = SendATPReq(&addr, requests++, requestBuff, theSize, &respSize);
- if (result != 0)
- errors++;
-
- if (theDelay)
- {
- ulong t;
-
- t = Ticks;
- while (t+theDelay >= Ticks);
- }
-
- bytes += theSize + respSize;
- if (bytes >= maxbytes)
- break;
- }
-
- time = Ticks - time;
- GetDateTime(&time3);
- time3 -= time2;
-
- if (requests)
- {
- printf("Request size %d, Response size %d\n",
- theSize, theBDSCount * MAX_RESP_SIZE);
- printf("requests %ld, errors %ld --> %d%%\n",
- requests, errors, (word)((errors/requests)*100.0));
-
- packets = requests * (theBDSCount + 1);
- }
-
- seconds = time / 60.0;
-
- /* low mem ticks */
- printf("ticks seconds %.02f\n", seconds);
-
- printf(" %ld bytes, %.03f seconds --> %.02f bytes/second\n",
- bytes, seconds, bytes / seconds);
- printf(" %ld packets --> %.02f packets/second\n",
- packets, packets / seconds);
-
- /* clock chip */
- printf("clock seconds %ld\n", time3);
- seconds = time3;
-
- printf(" %ld bytes, %.03f seconds --> %.02f bytes/second\n",
- bytes, seconds, bytes / seconds);
- printf(" %ld packets --> %.02f packets/second\n",
- packets, packets / seconds);
- }
- else
- printf("failed to find a server\n");
-
- if (nbp_iopb.ntQElPtr != 0)
- {
- DisposPtr(nbp_iopb.ntQElPtr);
- nbp_iopb.ntQElPtr = 0;
- }
-
- }
-
- ReceiveThem()
- {
- NetAddress addr;
- word respSize, i, result;
- ulong packets = 0,
- errors = 0;
- ulong user;
- uword tid;
-
- if (atpSocket == 0)
- atpSocket = ATPOpenSkt(0);
-
- addr.na_network = 0;
- addr.na_node = 0;
- addr.na_socket = atpSocket;
-
- if (MakeServer(&addr))
- {
- printf("waiting for packets...\n");
-
- while (!Button())
- {
- result = GetATPReq(&addr, &user, responseBuff, MAX_RESP_SIZE, &respSize, &tid);
- if (respSize == -1)
- break;
-
- if (result != 0)
- errors++;
- else
- {
- SendATPResp(&addr, tid, responseBuff, MAX_RESP_SIZE);
- }
-
- packets++;
- }
-
- if (packets > 0)
- printf("packets %ld, errors %ld --> %d%%\n",
- packets, errors, (word)((errors/packets)*100.0));
-
- KillServer();
- }
- else
- printf("unable to register server\n");
-
- if (nbp_iopb.ntQElPtr != 0)
- {
- DisposPtr(nbp_iopb.ntQElPtr);
- nbp_iopb.ntQElPtr = 0;
- }
- }
-
- byte *MakeEntity(object, type, zone)
- byte *object, *type, *zone;
- {
- byte *entity;
- byte *p;
- int nameSize;
-
- nameSize = strlen(object) + strlen(type) + strlen(zone) + 3;
- p = entity = (byte *)NewPtr( (long)nameSize );
-
- if (entity)
- {
- ctopcpy(p, object);
- p += strlen(object) + 1;
-
- ctopcpy(p, type);
- p += strlen(type) + 1;
-
- ctopcpy(p, zone);
- p += strlen(zone) + 1;
- }
-
- return entity;
- }
-
- FindServer(addr)
- NetAddress *addr;
- {
- word result;
- extern byte *MakeEntity();
- extern NBPComplete();
-
- nbp_iopb.ioCompletion = 0;
- nbp_iopb.ioRefNum = MPP_REFNUM;
- nbp_iopb.csCode = lookupName;
- nbp_iopb.interval = 5;
- nbp_iopb.count = 2;
-
- printf("looking in zone '%s' for server '%s'\n", theZone, theName);
- if (nbp_iopb.ntQElPtr == 0)
- nbp_iopb.ntQElPtr = MakeEntity(theName, NBP_SERVER_TYPE, theZone);
-
- if (lookupBuffer == (byte *)0)
- lookupBuffer = (byte *)NewPtr( (long)NBP_BUFFER_SIZE );
-
- nbp_iopb.nbp.lookup.retBuffPtr = lookupBuffer;
- nbp_iopb.nbp.lookup.retBuffSize = NBP_BUFFER_SIZE;
- nbp_iopb.nbp.lookup.maxToGet = 1000;
- nbp_iopb.nbp.lookup.numGotten = 0;
-
- printf("looking for server...\n");
- result = PBControl(&nbp_iopb, FALSE);
- if (result != noErr)
- return FALSE;
-
- printf("done, found %d servers\n",
- nbp_iopb.nbp.lookup.numGotten);
-
- {
- int i;
- int newCount = 0;
- byte *p = lookupBuffer;
- byte *name, *object, *zone;
- word network;
- byte node, socket;
-
- for (i = 0; i < nbp_iopb.nbp.lookup.numGotten; i++)
- {
- network = (p[0] << 8) | p[1];
- node = p[2];
- socket = p[3];
- name = p + 5;
- object = name + name[0] + 1;
- zone = object + object[0] + 1;
-
- if (1)
- {
- addr->na_network = network;
- addr->na_node = node;
- addr->na_socket = socket;
- return TRUE;
- }
-
- p = zone + zone[0] + 1;
- }
- }
-
- return FALSE;
- }
-
- MakeServer(addr)
- NetAddress *addr;
- {
- word result, i;
- Handle nameHandle;
- byte name[256];
-
- extern namesTableEntry *MakeNamesTableEntry();
-
- nbp_iopb.ioCompletion = 0;
- nbp_iopb.ioRefNum = MPP_REFNUM;
- nbp_iopb.csCode = registerName;
- nbp_iopb.interval = 10;
- nbp_iopb.count = 1;
-
- /* get user's chooser name for packet server name */
- nameHandle = GetResource('STR ', -16096);
- if (nameHandle == 0 || (*nameHandle)[0] == 0)
- strcpy(name, NBP_SERVER_TYPE);
- else
- ptoccpy(name, *nameHandle);
-
- /* register a name, bound to a ddp socket */
- nameEntry = MakeNamesTableEntry(name, NBP_SERVER_TYPE, "*");
-
- nameEntry->network = addr->na_network;
- nameEntry->node = addr->na_node;
- nameEntry->socket = addr->na_socket;
-
- nbp_iopb.ntQElPtr = (byte *)nameEntry;
- nbp_iopb.nbp.verifyFlag = -1;
-
- printf("registering server\n");
- result = PBControl(&nbp_iopb, FALSE);
- if (result != noErr)
- return FALSE;
-
- printf("done\n");
- return TRUE;
- }
-
- KillServer()
- {
- nbpParam iopb;
- word result, i;
-
- iopb.ioCompletion = 0;
- iopb.ioRefNum = MPP_REFNUM;
- iopb.csCode = removeName;
- iopb.ntQElPtr = nbp_iopb.ntQElPtr + 9;
-
- printf("removing server\n");
- result = PBControl(&iopb, FALSE);
- if (result != noErr)
- {
- printf("error %d\n", result);
- return FALSE;
- }
-
- printf("done\n");
- return TRUE;
- }
-
- SendATPReq(addr, user, pData, size, responseSize)
- NetAddress *addr;
- ulong user;
- byte *pData;
- word size;
- word *responseSize;
- {
- word result;
- atpParam iopb;
- register atpParam *pb = &iopb;
- register at_BDSType *b = bds;
- register int i;
-
- *responseSize = 0;
-
- if (atpSocket == 0)
- atpSocket = ATPOpenSkt(0);
-
- pb->ioRefNum = ATP_REFNUM;
- pb->csCode = sendRequest;
- pb->addrBlk.network = addr->na_network;
- pb->addrBlk.node = addr->na_node;
- pb->addrBlk.socket = addr->na_socket;
-
- pb->atpSkt = atpSocket;
- pb->atpFlgs = 0;
- pb->userData = user;
-
- pb->sendReqUnion.reqLength = size;
- pb->sendReqUnion.reqPointer = pData;
-
- pb->sendReqUnion.bdsPointer = bds;
- pb->sendReqUnion.numOfBuffs = theBDSCount;
- pb->sendReqUnion.timeOutVal = ATP_TIMEOUT;
- pb->sendReqUnion.retryCount = ATP_RETRY;
-
- for (i = 0; i < theBDSCount; i++, b++)
- {
- b->bsdBuffSz = MAX_RESP_SIZE;
- b->bdsBuffAddr = responseBuff;
- b->bdsDataSz = 0;
- }
-
- result = PBControl(&iopb, FALSE);
-
- for (i = 0, b = bds; i < theBDSCount; i++, b++)
- {
- *responseSize += b->bdsDataSz;
- }
-
- return result;
- }
-
- GetATPReq(addr, user, pData, size, requestSize, tid)
- NetAddress *addr;
- ulong *user;
- byte *pData;
- word size;
- word *requestSize, *tid;
- {
- word result;
- atpParam iopb;
- register atpParam *pb = &iopb;
-
- *requestSize = 0;
- *tid = 0;
-
- if (atpSocket == 0)
- atpSocket = ATPOpenSkt(0);
-
- pb->ioRefNum = ATP_REFNUM;
- pb->ioCompletion = 0;
- pb->csCode = getRequest;
-
- pb->addrBlk.network = 0;
- pb->addrBlk.node = 0;
- pb->addrBlk.socket = 0;
-
- pb->atpSkt = atpSocket;
- pb->atpFlgs = 0;
-
- pb->getReqUnion.reqLength = size;
- pb->getReqUnion.reqPointer = pData;
-
- result = PBControl(&iopb, TRUE);
- while (pb->ioResult > 0)
- {
- if (Button())
- {
- atpParam iopbKill;
-
- /* kill pending getRequest */
- iopbKill.ioRefNum = ATP_REFNUM;
- iopbKill.csCode = killGetReq;
- iopbKill.killReqUnion.killQEl = (struct atpRBlk *)&iopb;
-
- result = PBControl(&iopbKill, FALSE);
- printf("kill getRequest result %d\n", result);
- *requestSize = -1;
- return 0;
- }
- }
-
- *requestSize = pb->getReqUnion.reqLength;
- *tid = pb->getReqUnion.tid;
-
- addr->na_network = pb->addrBlk.network;
- addr->na_node = pb->addrBlk.node;
- addr->na_socket = pb->addrBlk.socket;
-
- return result;
- }
-
- SendATPResp(addr, tid, pData, size)
- NetAddress *addr;
- uword tid;
- byte *pData;
- word size;
- {
- word result;
- atpParam iopb;
- register atpParam *pb = &iopb;
- register at_BDSType *b = bds;
- register int i;
-
- if (atpSocket == 0)
- atpSocket = ATPOpenSkt(0);
-
- pb->ioRefNum = ATP_REFNUM;
- pb->csCode = sendResponse;
-
- pb->addrBlk.network = addr->na_network;
- pb->addrBlk.node = addr->na_node;
- pb->addrBlk.socket = addr->na_socket;
-
- pb->atpSkt = atpSocket;
- pb->atpFlgs = atpEOM;
-
- pb->sendRespUnion.bdsPointer = bds;
- pb->sendRespUnion.numOfBuffs = theBDSCount;
- pb->sendRespUnion.bdsSize = theBDSCount;
- pb->sendRespUnion.tid = tid;
-
- for (i = 0; i < theBDSCount; i++, b++)
- {
- b->bsdBuffSz = MAX_RESP_SIZE;
- b->bdsBuffAddr = responseBuff;
- b->bdsDataSz = 0;
- }
-
- result = PBControl(&iopb, FALSE);
-
- return result;
- }
-
- OpenAppleTalk()
- {
- word *myABusVar, refnum;
- myABusVar = (word *)0x2d8;
-
- OpenDriver("\p.MPP", &refnum);
-
- if( *myABusVar == -1 )
- {
- SysBeep(10);
- printf("Can't open AppleTalk drivers\n");
- }
- }
-
- int ATPOpenSkt(socket)
- word socket;
- {
- word result;
- atpParam iopb;
-
- iopb.ioRefNum = ATP_REFNUM;
- iopb.csCode = openATPSkt;
- iopb.atpSkt = socket;
- iopb.addrBlk.network = 0;
- iopb.addrBlk.node = 0;
- iopb.addrBlk.socket = 0;
-
- result = PBControl(&iopb, FALSE);
- if (result == noErr)
- return iopb.atpSkt;
-
- return 0;
- }
-
- namesTableEntry *MakeNamesTableEntry(object, type, zone)
- byte *object, *type, *zone;
- {
- namesTableEntry *entry;
- byte *p;
- int nameSize;
-
- nameSize = strlen(object) + strlen(type) + strlen(zone) + 3;
- entry = (namesTableEntry *)NewPtr( (long)sizeof(namesTableEntry) + nameSize );
-
- if (entry)
- {
- p = entry->names;
- ctopcpy(p, object);
- p += strlen(object) + 1;
-
- ctopcpy(p, type);
- p += strlen(type) + 1;
-
- ctopcpy(p, zone);
- p += strlen(zone) + 1;
- }
-
- return entry;
- }
-
-